package cn.cvs.controller;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.storageRecord.StroageRecordService;
import cn.cvs.service.supplier.SupplierService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/sys/storageRecord/")
public class StorageRecordController {
    @Resource
    private StroageRecordService stroageRecordService;
    @Resource
    private SupplierService supplierService;

    @RequestMapping("/list")
    public String toList(@RequestParam(value = "queryGoodsName",required = false)String queryGoodsName,
                         @RequestParam(value = "querySupplierId",required = false)Integer querySupplierId,
                         @RequestParam(value = "queryPayStatus",required = false)Integer queryPayStatus,
                         @RequestParam(value = "pageIndex", required = false) String pageIndex,
                         Model model)throws Exception{
        Integer currentPageNo = 1;
        if (pageIndex!=null && !"".equals(pageIndex)){
            currentPageNo = Integer.valueOf(pageIndex);
        }
        Integer pageSize = Constants.pageSize;
        PageSupport pageSupport = new PageSupport();
        pageSupport.setPageSize(pageSize);
        int totalCount = stroageRecordService.findCount(queryGoodsName, querySupplierId, queryPayStatus);
        pageSupport.setTotalCount(totalCount);
        int totalPageCount = pageSupport.getTotalPageCount();
        if (currentPageNo<1){
            currentPageNo = 1;
        }else if (currentPageNo>totalPageCount){
            currentPageNo = totalPageCount;
        }
        List<StorageRecord> storageRecordList = stroageRecordService.findStorageList(queryGoodsName, querySupplierId, queryPayStatus, currentPageNo, pageSize);
        model.addAttribute("storageRecordList",storageRecordList);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("querypayStatus",queryPayStatus);
        model.addAttribute("querySupplierId",querySupplierId);
        model.addAttribute("queryGoodsName",queryGoodsName);
        model.addAttribute("supplierList",supplierService.findAllSupplier());
        return "/storageRecord/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() throws Exception {

        return "storageRecord/add";
    }

    @PostMapping("/add")
    public String add(StorageRecord storageRecord,HttpSession session) throws Exception {
        SysUser sysUser = (SysUser) session.getAttribute("userSession");
        storageRecord.setCreatedUserId(sysUser.getId());
        //设置创建时间
        storageRecord.setCreatedTime(new Date());
        Boolean bool = stroageRecordService.addStorageRecord(storageRecord);
        if (bool){
            return "redirect:/sys/storageRecord/list";
        }
       return "storageRecord/add";
    }

    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(Model model, @PathVariable("id") int id) throws Exception {
        StorageRecord storageRecord = stroageRecordService.findStorageRecordById(id);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/update";
    }

    @PostMapping("/update")
    public String update(StorageRecord storageRecord,HttpSession session) throws Exception {
        SysUser sysUser = (SysUser) session.getAttribute("userSession");
        storageRecord.setUpdatedTime(new Date());
        storageRecord.setUpdatedUserId(sysUser.getId());
        Boolean bool = stroageRecordService.updateStorageRecord(storageRecord);
        if (bool){
            return "redirect:/sys/storageRecord/list";
        }
        return "storageRecord/update";
    }

    /**
     * 进入订单详情页面
     * @return
     */
    @RequestMapping("/view/{storageRecordId}")
    public String view(Model model, @PathVariable Integer storageRecordId) throws Exception {
        StorageRecord storageRecord = stroageRecordService.findStorageRecordById(storageRecordId);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/view";
    }

    /**
     * Ajax删除入库信息
     */
    @ResponseBody
    @DeleteMapping("/del/{storageRecordId}")
    public Object del(@PathVariable int storageRecordId) throws Exception {
        System.out.println("进入删除入库信息方法，参数"+storageRecordId);
        HashMap<String,Object> resultMap = new HashMap<String, Object>();
        StorageRecord storageRecord = stroageRecordService.findStorageRecordById(storageRecordId);
        if (storageRecord == null){//判断该用户是否存在
            resultMap.put("delResult","notexist");
        }else {
            if (stroageRecordService.deleteStorageRecordById(storageRecordId)){//判断是否删除成功
                resultMap.put("delResult","true");
            }else {
                resultMap.put("delResult","false");
            }
        }
        return JSON.toJSONString(resultMap);
    }
}
